package notifiers;

import dao.common.OrderManager;
import dao.hibernate.OrderManagerHB;
import models.entities.ProductEntity;
import models.entities.UserEntity;
import models.exceptions.GetEntityException;
import models.exceptions.GetEntityListException;
import models.repository.ProductRepositorySQL;
import models.repository.UserRepositorySQL;
import models.repository.common.ProductRepository;
import models.repository.common.UserRepository;
import models.shop.Mail.Message;
import models.shop.Mail.ProductQuestionMessage;
import models.shop.Order;
import models.shop.User;
import models.shop.UserProfile;
import play.i18n.Messages;
import play.mvc.Mailer;

import java.io.IOException;
import java.util.List;

public class Mails extends Mailer {
    public static void confirmOrder(int orderId, UserProfile user, String from, String body) throws GetEntityException, IOException {
        String subject = Messages.get("Order.confirmation.mails.subject", orderId);
        setSubject(subject);
        addRecipient(user.email);
        setFrom(from);
        OrderManager repository = new OrderManagerHB();
        Order order = repository.getById(orderId);
        send(order, body);
    }

    public static void confirmOrderForAdmin(int orderId, UserProfile user, List<User> admins) throws GetEntityException, IOException {
        String subject = Messages.get("Order.confirmation.mails.subject", orderId);
        setSubject(subject);
        for (User admin : admins){
            addRecipient(admin.userInfo.email);
        }
        setFrom(user.email);
        OrderManager repository = new OrderManagerHB();
        Order order = repository.getById(orderId);
        send(order, user);
    }

    public static void askQuestion(String username, String email, String body, Integer productId) throws GetEntityListException, IOException {
        askQuestion(new Message(username, email, body), productId);
    }

    public static void sendPasswordForExistentUser(UserEntity existentUser) {
        setSubject("Digitalband.ru - Напоминание пароля");
        addRecipient(existentUser.email);
        setFrom("info@digitalband.ru");
        send(existentUser);
    }

    public static void askQuestion(Message message, Integer productId) throws IOException, GetEntityListException {
        askQuestion(new ProductQuestionMessage(message, productId));
    }

    public static void askQuestion(ProductQuestionMessage message) throws IOException, GetEntityListException {
        ProductEntity product = null;
        if (message.productId != null) {
            ProductRepository productRepository = new ProductRepositorySQL();
            product = productRepository.getProduct(message.productId);
        }
        UserRepository userRepository = new UserRepositorySQL();
        List<String> adminEmails = userRepository.getAdminEmails();
        if (adminEmails == null || adminEmails.size() == 0){
            return;
        }
        String from = message.email;
        String subject = Messages.get("Mails.askquestion.subject");

        setSubject(subject);
        for(String adminEmail : adminEmails){
            if (adminEmail != null)
                addRecipient(adminEmail);
        }
        setFrom(from);
        send(message, product, message.productTitle);
    }
}